

<!DOCTYPE html>
<html class="writer-html5" lang="zh" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>体系结构概述 &mdash; Scrapy 2.3.0 文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.custom.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.bundle.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-shadow.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-punk.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-noir.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-light.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-borderless.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/micromodal.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/sphinx_rtd_theme.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/js/hoverxref.js"></script>
        <script src="../_static/js/tooltipster.bundle.min.js"></script>
        <script src="../_static/js/micromodal.min.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="下载器中间件" href="downloader-middleware.html" />
    <link rel="prev" title="asyncio" href="asyncio.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Scrapy
          

          
          </a>

          
            
            
              <div class="version">
                2.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">第一步</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/overview.html">Scrapy一目了然</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/tutorial.html">Scrapy 教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/examples.html">实例</a></li>
</ul>
<p class="caption"><span class="caption-text">基本概念</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="commands.html">命令行工具</a></li>
<li class="toctree-l1"><a class="reference internal" href="spiders.html">蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="selectors.html">选择器</a></li>
<li class="toctree-l1"><a class="reference internal" href="items.html">项目</a></li>
<li class="toctree-l1"><a class="reference internal" href="loaders.html">项目加载器</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Scrapy shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="item-pipeline.html">项目管道</a></li>
<li class="toctree-l1"><a class="reference internal" href="feed-exports.html">Feed 导出</a></li>
<li class="toctree-l1"><a class="reference internal" href="request-response.html">请求和响应</a></li>
<li class="toctree-l1"><a class="reference internal" href="link-extractors.html">链接提取器</a></li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">设置</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">例外情况</a></li>
</ul>
<p class="caption"><span class="caption-text">内置服务</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="logging.html">登录</a></li>
<li class="toctree-l1"><a class="reference internal" href="stats.html">统计数据集合</a></li>
<li class="toctree-l1"><a class="reference internal" href="email.html">发送电子邮件</a></li>
<li class="toctree-l1"><a class="reference internal" href="telnetconsole.html">远程登录控制台</a></li>
<li class="toctree-l1"><a class="reference internal" href="webservice.html">Web服务</a></li>
</ul>
<p class="caption"><span class="caption-text">解决具体问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
<li class="toctree-l1"><a class="reference internal" href="debug.html">调试spiders</a></li>
<li class="toctree-l1"><a class="reference internal" href="contracts.html">蜘蛛合约</a></li>
<li class="toctree-l1"><a class="reference internal" href="practices.html">常用做法</a></li>
<li class="toctree-l1"><a class="reference internal" href="broad-crawls.html">宽爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer-tools.html">使用浏览器的开发人员工具进行抓取</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic-content.html">选择动态加载的内容</a></li>
<li class="toctree-l1"><a class="reference internal" href="leaks.html">调试内存泄漏</a></li>
<li class="toctree-l1"><a class="reference internal" href="media-pipeline.html">下载和处理文件和图像</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">部署蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="autothrottle.html">AutoThrottle 扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">标杆管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="jobs.html">作业：暂停和恢复爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutines.html">协同程序</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio.html">asyncio</a></li>
</ul>
<p class="caption"><span class="caption-text">扩展Scrapy</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">体系结构概述</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#overview">概述</a></li>
<li class="toctree-l2"><a class="reference internal" href="#data-flow">数据流</a></li>
<li class="toctree-l2"><a class="reference internal" href="#components">组件</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#scrapy-engine">抓取式发动机</a></li>
<li class="toctree-l3"><a class="reference internal" href="#scheduler">调度程序</a></li>
<li class="toctree-l3"><a class="reference internal" href="#downloader">下载器</a></li>
<li class="toctree-l3"><a class="reference internal" href="#spiders">蜘蛛</a></li>
<li class="toctree-l3"><a class="reference internal" href="#item-pipeline">项目管道</a></li>
<li class="toctree-l3"><a class="reference internal" href="#downloader-middlewares">下载器中心件</a></li>
<li class="toctree-l3"><a class="reference internal" href="#spider-middlewares">蜘蛛中心件</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#event-driven-networking">事件驱动的网络</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="downloader-middleware.html">下载器中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="spider-middleware.html">蜘蛛中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">核心API</a></li>
<li class="toctree-l1"><a class="reference internal" href="signals.html">信号</a></li>
<li class="toctree-l1"><a class="reference internal" href="exporters.html">条目导出器</a></li>
</ul>
<p class="caption"><span class="caption-text">其余所有</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../news.html">发行说明</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">为 Scrapy 贡献</a></li>
<li class="toctree-l1"><a class="reference internal" href="../versioning.html">版本控制和API稳定性</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Scrapy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>体系结构概述</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="architecture-overview">
<span id="topics-architecture"></span><h1>体系结构概述<a class="headerlink" href="#architecture-overview" title="永久链接至标题">¶</a></h1>
<p>本文描述了Scrapy的体系结构及其组件如何交互。</p>
<div class="section" id="overview">
<h2>概述<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
<p>下图显示了Scrapy架构及其组件的概述，以及系统内部发生的数据流的概要（以红色箭头显示）。下面提供了这些组件的简要说明以及有关它们的详细信息的链接。数据流也描述如下。</p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1466963416408457"
     data-ad-slot="8850786025"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div class="section" id="data-flow">
<span id="id1"></span><h2>数据流<a class="headerlink" href="#data-flow" title="永久链接至标题">¶</a></h2>
<a class="reference internal image-reference" href="../_images/scrapy_architecture_02.png"><img alt="Scrapy architecture" src="../_images/scrapy_architecture_02.png" style="width: 700px; height: 470px;" /></a>
<p>Scrapy中的数据流由执行引擎控制，如下所示：</p>
<ol class="arabic simple">
<li><p>这个 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> 获取要从 <a class="reference internal" href="#component-spiders"><span class="std std-ref">Spider</span></a> .</p></li>
<li><p>这个 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> 在中安排请求 <a class="reference internal" href="#component-scheduler"><span class="std std-ref">Scheduler</span></a> 并请求下一个要爬行的请求。</p></li>
<li><p>这个 <a class="reference internal" href="#component-scheduler"><span class="std std-ref">Scheduler</span></a> 将下一个请求返回到 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> .</p></li>
<li><p>这个 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> 将请求发送到 <a class="reference internal" href="#component-downloader"><span class="std std-ref">Downloader</span></a> ，通过 <a class="reference internal" href="#component-downloader-middleware"><span class="std std-ref">Downloader Middlewares</span></a> （见 <a class="reference internal" href="downloader-middleware.html#scrapy.downloadermiddlewares.DownloaderMiddleware.process_request" title="scrapy.downloadermiddlewares.DownloaderMiddleware.process_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">process_request()</span></code></a> ）</p></li>
<li><p>一旦页面完成下载， <a class="reference internal" href="#component-downloader"><span class="std std-ref">Downloader</span></a> 生成响应（使用该页）并将其发送到引擎，并通过 <a class="reference internal" href="#component-downloader-middleware"><span class="std std-ref">Downloader Middlewares</span></a> （见 <a class="reference internal" href="downloader-middleware.html#scrapy.downloadermiddlewares.DownloaderMiddleware.process_response" title="scrapy.downloadermiddlewares.DownloaderMiddleware.process_response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">process_response()</span></code></a> ）</p></li>
<li><p>这个 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> 接收来自的响应 <a class="reference internal" href="#component-downloader"><span class="std std-ref">Downloader</span></a> 并发送到 <a class="reference internal" href="#component-spiders"><span class="std std-ref">Spider</span></a> 用于处理，通过 <a class="reference internal" href="#component-spider-middleware"><span class="std std-ref">Spider Middleware</span></a> （见 <a class="reference internal" href="spider-middleware.html#scrapy.spidermiddlewares.SpiderMiddleware.process_spider_input" title="scrapy.spidermiddlewares.SpiderMiddleware.process_spider_input"><code class="xref py py-meth docutils literal notranslate"><span class="pre">process_spider_input()</span></code></a> ）</p></li>
<li><p>这个 <a class="reference internal" href="#component-spiders"><span class="std std-ref">Spider</span></a> 处理响应并向 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> ，通过 <a class="reference internal" href="#component-spider-middleware"><span class="std std-ref">Spider Middleware</span></a> （见 <a class="reference internal" href="spider-middleware.html#scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output" title="scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output"><code class="xref py py-meth docutils literal notranslate"><span class="pre">process_spider_output()</span></code></a> ）</p></li>
<li><p>这个 <a class="reference internal" href="#component-engine"><span class="std std-ref">Engine</span></a> 将已处理的项目发送到 <a class="reference internal" href="#component-pipelines"><span class="std std-ref">Item Pipelines</span></a> ，然后将已处理的请求发送到 <a class="reference internal" href="#component-scheduler"><span class="std std-ref">Scheduler</span></a> 并请求可能的下一个爬行请求。</p></li>
<li><p>该过程重复（从步骤1开始），直到不再有来自 <a class="reference internal" href="#component-scheduler"><span class="std std-ref">Scheduler</span></a> .</p></li>
</ol>
</div>
<div class="section" id="components">
<h2>组件<a class="headerlink" href="#components" title="永久链接至标题">¶</a></h2>
<div class="section" id="scrapy-engine">
<span id="component-engine"></span><h3>抓取式发动机<a class="headerlink" href="#scrapy-engine" title="永久链接至标题">¶</a></h3>
<p>引擎负责控制系统所有组件之间的数据流，并在发生某些操作时触发事件。见 <a class="reference internal" href="#data-flow"><span class="std std-ref">Data Flow</span></a> 有关详细信息，请参阅上面的部分。</p>
</div>
<div class="section" id="scheduler">
<span id="component-scheduler"></span><h3>调度程序<a class="headerlink" href="#scheduler" title="永久链接至标题">¶</a></h3>
<p>调度器接收来自引擎的请求，并将它们排队，以便在引擎请求时（也向引擎）提供这些请求。</p>
</div>
<div class="section" id="downloader">
<span id="component-downloader"></span><h3>下载器<a class="headerlink" href="#downloader" title="永久链接至标题">¶</a></h3>
<p>下载者负责获取网页并将其送入引擎，引擎反过来又将网页送入蜘蛛。</p>
</div>
<div class="section" id="spiders">
<span id="component-spiders"></span><h3>蜘蛛<a class="headerlink" href="#spiders" title="永久链接至标题">¶</a></h3>
<p>spider是Scrapy用户编写的自定义类，用于解析响应和提取 <a class="reference internal" href="items.html#topics-items"><span class="std std-ref">items</span></a> 从他们或其他要求跟随。有关详细信息，请参阅 <a class="reference internal" href="spiders.html#topics-spiders"><span class="std std-ref">蜘蛛</span></a> .</p>
</div>
<div class="section" id="item-pipeline">
<span id="component-pipelines"></span><h3>项目管道<a class="headerlink" href="#item-pipeline" title="永久链接至标题">¶</a></h3>
<p>项目管道负责处理被蜘蛛提取（或 爬取  ）的项目。典型的任务包括清理、验证和持久性（如将项目存储在数据库中）。有关详细信息，请参阅 <a class="reference internal" href="item-pipeline.html#topics-item-pipeline"><span class="std std-ref">项目管道</span></a> .</p>
</div>
<div class="section" id="downloader-middlewares">
<span id="component-downloader-middleware"></span><h3>下载器中心件<a class="headerlink" href="#downloader-middlewares" title="永久链接至标题">¶</a></h3>
<p>下载器中间件是位于引擎和下载器之间的特定钩子，当它们从引擎传递到下载器时处理请求，以及从下载器传递到引擎的响应。</p>
<p>如果需要执行以下操作之一，请使用下载器中间件：</p>
<ul class="simple">
<li><p>在将请求发送给下载者之前处理该请求（即在Scrapy将请求发送到网站之前）；</p></li>
<li><p>变更在传递给spider之前收到响应；</p></li>
<li><p>发送新的请求，而不是将收到的响应传递给spider；</p></li>
<li><p>在不获取网页的情况下将响应传递给蜘蛛；</p></li>
<li><p>悄悄地放弃一些请求。</p></li>
</ul>
<p>有关详细信息，请参阅 <a class="reference internal" href="downloader-middleware.html#topics-downloader-middleware"><span class="std std-ref">下载器中间件</span></a> .</p>
</div>
<div class="section" id="spider-middlewares">
<span id="component-spider-middleware"></span><h3>蜘蛛中心件<a class="headerlink" href="#spider-middlewares" title="永久链接至标题">¶</a></h3>
<p>蜘蛛中间件是位于引擎和蜘蛛之间的特定钩子，能够处理蜘蛛的输入（响应）和输出（项目和请求）。</p>
<p>如果需要，使用蜘蛛中间件</p>
<ul class="simple">
<li><p>spider回调的后处理输出-更改/添加/删除请求或项；</p></li>
<li><p>后处理启动请求；</p></li>
<li><p>处理spider异常；</p></li>
<li><p>对一些基于响应内容的请求调用errback，而不是回调。</p></li>
</ul>
<p>有关详细信息，请参阅 <a class="reference internal" href="spider-middleware.html#topics-spider-middleware"><span class="std std-ref">蜘蛛中间件</span></a> .</p>
</div>
</div>
<div class="section" id="event-driven-networking">
<h2>事件驱动的网络<a class="headerlink" href="#event-driven-networking" title="永久链接至标题">¶</a></h2>
<p>Scrapy是用 <a class="reference external" href="https://twistedmatrix.com/trac/">Twisted</a> 是一个流行的事件驱动的python网络框架。因此，它使用非阻塞（即异步）代码实现并发性。</p>
<p>有关异步编程和扭曲的更多信息，请参阅以下链接：</p>
<ul class="simple">
<li><p><a class="reference external" href="https://twistedmatrix.com/documents/current/core/howto/defer-intro.html" title="(在 Twisted v20.3)"><span>Introduction to Deferreds</span></a></p></li>
<li><p><a class="reference external" href="http://jessenoller.com/blog/2009/02/11/twisted-hello-asynchronous-programming/">Twisted - hello, asynchronous programming</a></p></li>
<li><p><a class="reference external" href="http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/">Twisted Introduction - Krondo</a></p></li>
</ul>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="downloader-middleware.html" class="btn btn-neutral float-right" title="下载器中间件" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="asyncio.html" class="btn btn-neutral float-left" title="asyncio" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2008–2020, Scrapy developers
      <span class="lastupdated">
        最后更新于 10月 18, 2020.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
 
<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
analytics.load("8UDQfnf3cyFSTsM4YANnW5sXmgZVILbA");
analytics.page();
}}();

analytics.ready(function () {
    ga('require', 'linker');
    ga('linker:autoLink', ['scrapinghub.com', 'crawlera.com']);
});
</script>


</body>
</html>